package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsBrandMapper;
import com.qf.pojo.DtsBrand;
import com.qf.service.BrandService;
import com.qf.vo.CatVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
public class BrandServiceImpl implements BrandService {
    @Autowired
    private DtsBrandMapper dtsBrandMapper;


    @Override
    public List<CatVo> findAll() {
        return dtsBrandMapper.findAll();
    }

    @Override
    public IPage<DtsBrand> findListByQuery(Integer page, Integer limit, String name, Integer id, String sort, String order) {
        return dtsBrandMapper.selectPage(new Page<>(page,limit),new QueryWrapper<DtsBrand>()
                .like(!StringUtils.isEmpty(name),"name",name)
                .eq(id!=null,"id",id)
                .eq("deleted",false)
                .orderBy(!StringUtils.isEmpty(sort),"ASC".equalsIgnoreCase(order),sort));

    }

    @Override
    public void create(DtsBrand dtsBrand) {
        dtsBrand.setAddTime(new Date());
        dtsBrand.setUpdateTime(new Date());
        dtsBrand.setDeleted(false);
        dtsBrandMapper.insert(dtsBrand);
    }

    @Override
    public void update(DtsBrand dtsBrand) {
        dtsBrand.setUpdateTime(new Date());
        dtsBrandMapper.updateById(dtsBrand);
    }

    @Override
    public void delete(DtsBrand dtsBrand) {
        dtsBrand.setDeleted(true);
        dtsBrandMapper.updateById(dtsBrand);
    }
}
